/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame.HangHoa;

import dao.DanhMucDAO;
import dao.DonViDAO;
import dao.SanPhamDAO;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import myclass.MyAdminFrame;
import myclass.NumberRenderer;
import pojo.Danhmuc;
import pojo.Donvi;
import pojo.Nhanvien;
import pojo.Sanpham;

/**
 *
 * @author Quan
 */
public class QLSanPham extends MyAdminFrame {

    private DefaultTableModel tablemodel;

    /**
     * Creates new form QLSanPham
     */
    public QLSanPham(Nhanvien nv) {
        super(nv);
        
        initComponents();

        tablemodel = new DefaultTableModel();
        tablemodel.addColumn("ID");
        tablemodel.addColumn("Mã sản phẩm");
        tablemodel.addColumn("Tên sản phẩm");
        tablemodel.addColumn("Danh mục");
        tablemodel.addColumn("Đơn giá");
        tablemodel.addColumn("Số lượng tồn");
        tablemodel.addColumn("Đơn vị tính");
        jtsanpham.setModel(tablemodel);

        SanPhamDAO spdao = new SanPhamDAO();
        try {
            LoadSanPham((List<Sanpham>) spdao.LayDanhSach());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(QLSanPham.class.getName()).log(Level.SEVERE, null, ex);
        }

        LoadDonVi();
        LoadDanhMuc();

        jtsanpham.getColumnModel().getColumn(4).setCellRenderer(NumberRenderer.getIntegerRenderer());
        jtsanpham.getColumnModel().getColumn(5).setCellRenderer(NumberRenderer.getIntegerRenderer());
    }

    private void LoadDonVi() {
        jcbb_donvitinh.removeAllItems();
        jcbb_donvitinh.addItem(null);
        jcbb_donvi.removeAllItems();
        jcbb_donvi.addItem(null);

        DonViDAO dvdao = new DonViDAO();
        List<Donvi> ds = null;
        try {
            ds = dvdao.LayDanhSach();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(QLSanPham.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ds != null) {
            for (int i = 0; i < ds.size(); i++) {
                pojo.Donvi donvi = ds.get(i);
                jcbb_donvitinh.addItem(donvi);
                jcbb_donvi.addItem(donvi);
            }
        }
    }

    private void LoadDanhMuc() {
        jcbb_danhmuc.removeAllItems();
        jcbb_danhmuc.addItem(null);
        jcbb_s_danhmuc.removeAllItems();
        jcbb_s_danhmuc.addItem(null);
        DanhMucDAO dmdao = new DanhMucDAO();
        List dsdm = null;
        try {
            dsdm = dmdao.LayDanhSach();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(QLDanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (dsdm != null) {
            for (int i = 0; i < dsdm.size(); i++) {
                jcbb_danhmuc.addItem(dsdm.get(i));
                jcbb_s_danhmuc.addItem(dsdm.get(i));
            }
        }
    }

    private void LoadSanPham(List<Sanpham> dssp) {
        tablemodel.getDataVector().clear();
        jtsanpham.removeAll();
        if (dssp != null) {
            for (int i = 0; i < dssp.size(); i++) {
                Sanpham sanpham = dssp.get(i);
                tablemodel.addRow(new Object[]{
                            sanpham.getId(),
                            sanpham.getMasanpham(),
                            sanpham,
                            sanpham.getDanhmuc(),
                            (long) sanpham.getDongia(),
                            (long) sanpham.getSoluongton(),
                            sanpham.getDonvi()
                        });
            }
        }
    }

    private void Tim() {
        Map<String, Object> map = new HashMap<>();
        if (jtf_s_masp.getText().length() >= 0) {
            map.put("masp", jtf_s_masp.getText());
        }
        try {
            map.put("sltonlonhon", Long.valueOf(jtf_s_slton.getText()));
        } catch (Exception e) {
        }
        try {
            map.put("sltonnhohon", Long.valueOf(jtf_s_sltonnhohon.getText()));
        } catch (Exception e) {
        }
        try {
        } catch (Exception e) {
        }
        map.put("ten", jtf_s_tensanpham.getText());
        map.put("danhmuc", jcbb_s_danhmuc.getSelectedItem());
        map.put("donvi", jcbb_donvi.getSelectedItem());
        LoadSanPham(SanPhamDAO.Tim(map));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtsanpham = new javax.swing.JTable();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtf_id = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtf_masanpham = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtf_tensanpham = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jcbb_danhmuc = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jtf_dongia = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jcbb_donvitinh = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jtf_s_masp = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jtf_s_slton = new javax.swing.JTextField();
        jtf_s_sltonnhohon = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jtf_s_tensanpham = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jcbb_s_danhmuc = new javax.swing.JComboBox();
        jcbb_donvi = new javax.swing.JComboBox();
        jButton5 = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Sản phẩm");

        jtsanpham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtsanpham.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtsanphamMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtsanpham);

        jButton4.setText("Thêm mới");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton3.setText("Sửa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton2.setText("Xóa");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Đóng");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin sản phẩm"));

        jLabel1.setText("ID");

        jtf_id.setEditable(false);

        jLabel2.setText("Mã sản phẩm");

        jLabel3.setText("Tên sản phẩm");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtf_id)
                    .addComponent(jtf_tensanpham)
                    .addComponent(jtf_masanpham))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtf_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtf_masanpham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtf_tensanpham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel4.setText("Danh mục");

        jLabel5.setText("Đơn giá");

        jLabel6.setText("Đơn vị tính");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addGap(25, 25, 25)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbb_danhmuc, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jtf_dongia)
                    .addComponent(jcbb_donvitinh, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jcbb_danhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtf_dongia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jcbb_donvitinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin tìm kiếm"));

        jPanel5.setPreferredSize(new java.awt.Dimension(150, 123));

        jLabel7.setText("Mã sản phẩm");

        jLabel8.setText("SL tồn lớn hơn");

        jLabel9.setText("SL tồn nhỏ hơn");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addGap(34, 34, 34)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtf_s_masp)
                    .addComponent(jtf_s_slton)
                    .addComponent(jtf_s_sltonnhohon))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtf_s_masp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jtf_s_slton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jtf_s_sltonnhohon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        jPanel6.setPreferredSize(new java.awt.Dimension(300, 123));

        jLabel10.setText("Tên sản phẩm");

        jLabel11.setText("Danh mục");

        jLabel12.setText("Đon vị tính");

        jButton5.setText("Tìm");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton5))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbb_donvi, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jcbb_s_danhmuc, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jtf_s_tensanpham))))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jtf_s_tensanpham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jcbb_s_danhmuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jcbb_donvi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addGap(168, 168, 168)
                        .addComponent(jButton1))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        Sanpham sp = new Sanpham();
        sp.setMasanpham(jtf_masanpham.getText());
        sp.setTensanpham(jtf_tensanpham.getText());
        try {
            sp.setDongia(Long.valueOf(jtf_dongia.getText()));
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
        }

        if (jcbb_danhmuc.getSelectedItem() != null) {
            sp.setDanhmuc((Danhmuc) jcbb_danhmuc.getSelectedItem());
        }
        if (jcbb_donvitinh.getSelectedItem() != null) {
            sp.setDonvi((Donvi) jcbb_donvitinh.getSelectedItem());
        }

        sp.setNhanvien(nv);
        sp.setNgaynhapkho(new Date());
        sp.setSoluongton(0);
        SanPhamDAO spdao = new SanPhamDAO();
        try {
            spdao.Them(sp);
            LoadSanPham(spdao.LayDanhSach());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(QLSanPham.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jtsanphamMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtsanphamMouseClicked
        // TODO add your handling code here:
        if (jtsanpham.getSelectedRow() != -1) {
            int selected_index = jtsanpham.getSelectedRow();
            try {
                jtf_id.setText(tablemodel.getValueAt(selected_index, 0).toString());
                jtf_masanpham.setText(tablemodel.getValueAt(selected_index, 1).toString());
                jtf_tensanpham.setText(tablemodel.getValueAt(selected_index, 2).toString());

                Danhmuc dm = (Danhmuc) tablemodel.getValueAt(selected_index, 3);
                Donvi dv = (Donvi) tablemodel.getValueAt(selected_index, 6);

                for (int j = 1; j < jcbb_danhmuc.getItemCount(); j++) {
                    if (dm.getId() == ((Danhmuc) jcbb_danhmuc.getItemAt(j)).getId()) {
                        jcbb_danhmuc.setSelectedIndex(j);
                        break;
                    }
                }

                for (int j = 1; j < jcbb_donvitinh.getItemCount(); j++) {
                    if (dv.getId() == ((Donvi) jcbb_donvitinh.getItemAt(j)).getId()) {
                        jcbb_donvitinh.setSelectedIndex(j);
                        break;
                    }
                }
                jtf_dongia.setText(tablemodel.getValueAt(selected_index, 4).toString());

            } catch (Exception e) {
            }
        }
    }//GEN-LAST:event_jtsanphamMouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if (Long.valueOf(jtf_id.getText()) > 0) {
            SanPhamDAO spdao = new SanPhamDAO();
            Sanpham sp = null;
            try {
                sp = (Sanpham) spdao.Lay(Long.valueOf(jtf_id.getText()));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(QLSanPham.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (sp != null) {
                sp.setMasanpham(jtf_masanpham.getText());
                sp.setTensanpham(jtf_tensanpham.getText());
                sp.setDongia(Long.valueOf(jtf_dongia.getText()));
                sp.setDonvi((Donvi) jcbb_donvitinh.getSelectedItem());
                sp.setDanhmuc((Danhmuc) jcbb_danhmuc.getSelectedItem());
                try {
                    spdao.Sua(sp);
                    LoadSanPham(spdao.LayDanhSach());
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
                    Logger.getLogger(QLSanPham.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (Long.valueOf(jtf_id.getText()) > 0) {
            SanPhamDAO spdao = new SanPhamDAO();
            try {
                Sanpham sp = (Sanpham) spdao.Lay(Long.valueOf(jtf_id.getText()));
                spdao.Xoa(sp);
                LoadSanPham(spdao.LayDanhSach());
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Thông báo", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(QLSanPham.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        Tim();
    }//GEN-LAST:event_jButton5ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QLSanPham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new QLSanPham(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbb_danhmuc;
    private javax.swing.JComboBox jcbb_donvi;
    private javax.swing.JComboBox jcbb_donvitinh;
    private javax.swing.JComboBox jcbb_s_danhmuc;
    private javax.swing.JTextField jtf_dongia;
    private javax.swing.JTextField jtf_id;
    private javax.swing.JTextField jtf_masanpham;
    private javax.swing.JTextField jtf_s_masp;
    private javax.swing.JTextField jtf_s_slton;
    private javax.swing.JTextField jtf_s_sltonnhohon;
    private javax.swing.JTextField jtf_s_tensanpham;
    private javax.swing.JTextField jtf_tensanpham;
    private javax.swing.JTable jtsanpham;
    // End of variables declaration//GEN-END:variables
}
